package com.zxw.ui;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.sevenheaven.segmentcontrol.SegmentControl;
import com.zxw.R;

import androidx.annotation.IntDef;
import androidx.annotation.StringRes;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class LightController {

    private static final String TAG = LightController.class.getSimpleName();

    // --------------------------------------------------------------------
    // DUTY
    public static final int DUTY_05_05 = 0;
    public static final int DUTY_25_75 = 1;
    public static final int DUTY_10_05 = 2;
    public static final int DUTY_10_10 = 3;
    public static final int DUTY_ALWAYS = 4;

    @IntDef({DUTY_05_05, DUTY_25_75, DUTY_10_05, DUTY_10_10, DUTY_ALWAYS})
    @Retention(RetentionPolicy.SOURCE)
    public @interface DUTY_VALUES {}

    //
    private final ILightControlDelegate delegate;

    private final TextView titleView;
    private final SeekBar daySegment;
    private final SeekBar nightSegment;
    private final TextView textDayLight;
    private final TextView textNightLight;
    private final View dutyLayout;
    private final SegmentControl dutySegment;

    private int duty = DUTY_05_05;
    private int dayLight = 50;
    private int nightLight = 50;

    public LightController(View rootView, ILightControlDelegate delegate) {
        this.delegate = delegate;
        titleView = rootView.findViewById(R.id.title_text_view);
        daySegment = rootView.findViewById(R.id.seek_bar_day);
        nightSegment = rootView.findViewById(R.id.seek_bar_night);
        textDayLight = rootView.findViewById(R.id.text_day_light);
        textNightLight = rootView.findViewById(R.id.text_night_light);
        dutyLayout = rootView.findViewById(R.id.duty_layout);
        dutySegment = rootView.findViewById(R.id.segment_control_duty);
        init();
    }

    @SuppressLint("SetTextI18n")
    private void init() {
        //
        daySegment.setMax(100);
        daySegment.setOnSeekBarChangeListener(new SeekBarListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                dayLight = progress;
                textDayLight.setText("" + dayLight);
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "select day light --- " + seekBar.getProgress());
                dayLight = seekBar.getProgress();
                textDayLight.setText("" + dayLight);
                if (delegate != null) {
                    delegate.onLightControlChanged();
                }
            }
        });
        daySegment.setProgress(dayLight);

        nightSegment.setMax(100);
        nightSegment.setOnSeekBarChangeListener(new SeekBarListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                nightLight = progress;
                textNightLight.setText("" + nightLight);
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "select day light --- " + seekBar.getProgress());
                nightLight = seekBar.getProgress();
                textNightLight.setText("" + nightLight);
                if (delegate != null) {
                    delegate.onLightControlChanged();
                }
            }
        });
        nightSegment.setProgress(nightLight);

        //
        setDuty(duty);
        dutySegment.setOnSegmentControlClickListener(index -> {
            Log.d(TAG, "select ch1 duty --- " + index);
            duty = index;
            if (this.delegate != null) {
                this.delegate.onLightControlChanged();
            }
        });
    }

    public void setTitle(@StringRes int title) {
        titleView.setText(title);
    }

    public int getDayLight() {
        return dayLight;
    }

    public void setDayLight(int light) {
        if (light < 0 || light > 100 || dayLight == light) {
            return;
        }
        dayLight = light;
        daySegment.setProgress(light);
    }

    public int getNightLight() {
        return nightLight;
    }

    public void setNightLight(int light) {
        if (light < 0 || light > 100 || nightLight == light) {
            return;
        }
        nightLight = light;
        nightSegment.setProgress(light);
    }

    public @DUTY_VALUES int getDuty() {
        return duty;
    }

    public void setDuty(@DUTY_VALUES int value) {
        dutySegment.setSelectedIndex(value);
        duty = value;
    }

    public void setDutyVisible(boolean visible) {
        dutyLayout.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    private interface SeekBarListener extends SeekBar.OnSeekBarChangeListener {
        default void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}
        default void onStartTrackingTouch(SeekBar seekBar) {}
    }

    public interface ILightControlDelegate {
        void onLightControlChanged();
    }
}
